package com.lesserpand.rabbitmq.rabbitmq.controller;

import com.lesserpand.rabbitmq.rabbitmq.bean.dto.OrderDTO;
import com.lesserpand.rabbitmq.rabbitmq.service.OrderService;
import com.lesserpand.rabbitmq.rabbitmq.service.RabbitmqInitSkillService;
import com.lesserpand.rabbitmq.rabbitmq.service.RabbitmqSkillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author bailuo
 * @version 2.0
 * @Description TODO
 * @Date Created in 2019/1/8 19:16
 */
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private RabbitmqSkillService rabbitmqSkillService;

    @Autowired
    private RabbitmqInitSkillService rabbitmqInitSkillService;

    /**
     * 模拟下单
     *
     * @param dto
     */
    @PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public void save(@RequestBody OrderDTO dto) {
        orderService.save(dto);
    }

    /**
     * 模拟支付
     *
     * @param id 订单id
     * @return
     */
    @PutMapping(value = "/payment", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Object payment(Long id) {
        OrderDTO dto = new OrderDTO();
        dto.setId(id);
        dto.setOrderStatus(new Byte("2"));
        return orderService.payment(dto);
    }

    /**
     * 手动初始化抢购数据
     *
     * @param proId
     * @return
     */
    @GetMapping(value = "/init-skill")
    public String initSkill(@RequestParam("proId") Long proId, @RequestParam("skillId") Long skillId, @RequestParam("num") String num) {
        return rabbitmqInitSkillService.initSkill(proId, skillId, num);
    }

    /**
     * 模拟抢购
     *
     * @param userId
     * @param proId
     * @return
     */
    @PostMapping(value = "/skill", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    public String skill(@RequestParam("userId") Long userId, @RequestParam("skillId") Long skillId, @RequestParam("proId") Long proId) {
        return rabbitmqSkillService.skill(userId, skillId, proId);
    }
}
